# -*- coding: utf-8 -*-

import wx
import WbEditPanel
import WbHtmlPanel
import WbEditDialog
import WbAboutDialog

__all__ = ['create']

def create(parent=None):
    return WbMainForm(parent)

[wxID_MAINFORM, wxID_MAINFORMBOX, wxID_MAINFORMDIRLIST, wxID_MAINFORMEP, 
 wxID_MAINFORMMODIFY, wxID_MAINFORMPANEL1, wxID_MAINFORMPANEL2, 
 wxID_MAINFORMSTATICTEXT1, wxID_MAINFORMSTATUSBAR1, 
] = [wx.NewId() for _init_ctrls in range(9)]

[wxID_MAINFORMHELPITEMABOUT] = [wx.NewId() for _init_coll_help_Items in range(1)]

class WbMainForm(wx.Frame):
    def _init_coll_boxSizer5_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.modify, 0, border=5, flag=wx.ALL)

    def _init_coll_boxSizer3_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.staticText1, 0, border=0, flag=0)
        parent.AddWindow(self.dirlist, 1, border=0, flag=wx.EXPAND)

    def _init_coll_boxSizer4_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.ep, 0, border=10, flag=wx.ALL)
        parent.AddWindow(self.staticLine1, 0, border=10,
              flag=wx.EXPAND | wx.LEFT | wx.RIGHT)
        parent.AddSizer(self.boxSizer5, 0, border=5,
              flag=wx.ALL | wx.ALIGN_RIGHT)

    def _init_coll_boxSizer1_Items(self, parent):
        # generated method, don't edit

        parent.AddWindow(self.panel1, 0, border=0, flag=wx.EXPAND)
        parent.AddWindow(self.panel2, 1, border=0, flag=wx.EXPAND)

    def _init_coll_boxSizer2_Items(self, parent):
        # generated method, don't edit

        parent.AddSizer(self.boxSizer3, 2, border=10, flag=wx.EXPAND | wx.ALL)
        parent.AddSizer(self.boxSizer4, 3, border=10,
              flag=wx.ALIGN_CENTER_VERTICAL | wx.ALL)

    def _init_coll_menuBar1_Menus(self, parent):
        # generated method, don't edit

        parent.Append(menu=self.help, title=_("&Help"))

    def _init_coll_help_Items(self, parent):
        # generated method, don't edit

        parent.Append(help='', id=wxID_MAINFORMHELPITEMABOUT,
              kind=wx.ITEM_NORMAL, text=_("&About"))
        self.Bind(wx.EVT_MENU, self.OnHelpItemaboutMenu,
              id=wxID_MAINFORMHELPITEMABOUT)

    def _init_sizers(self):
        # generated method, don't edit
        self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)

        self.boxSizer2 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self.boxSizer3 = wx.BoxSizer(orient=wx.VERTICAL)

        self.boxSizer4 = wx.StaticBoxSizer(self.box, orient=wx.VERTICAL)

        self.boxSizer5 = wx.BoxSizer(orient=wx.HORIZONTAL)

        self._init_coll_boxSizer1_Items(self.boxSizer1)
        self._init_coll_boxSizer2_Items(self.boxSizer2)
        self._init_coll_boxSizer3_Items(self.boxSizer3)
        self._init_coll_boxSizer4_Items(self.boxSizer4)
        self._init_coll_boxSizer5_Items(self.boxSizer5)

        self.SetSizer(self.boxSizer1)
        self.panel2.SetSizer(self.boxSizer2)
        #self.panel2.Fit()

    def _init_utils(self):
        # generated method, don't edit
        self.menuBar1 = wx.MenuBar()

        self.help = wx.Menu()

        self._init_coll_menuBar1_Menus(self.menuBar1)
        self._init_coll_help_Items(self.help)

    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_MAINFORM, name='MainForm', parent=prnt,
              style=wx.DEFAULT_FRAME_STYLE, title="MFT(Modify File Time)")
        self._init_utils()
        self.SetMenuBar(self.menuBar1)

        self.statusBar1 = wx.StatusBar(id=wxID_MAINFORMSTATUSBAR1,
              name='statusBar1', parent=self, style=0)
        self.statusBar1.SetFieldsCount(2)
        self.statusBar1.SetStatusWidths([-1, 150])
        self.statusBar1.SetStatusText(_("Welcome..."), 0)
        self.SetStatusBar(self.statusBar1)
        self.timer = wx.PyTimer(self.Notify)
        self.timer.Start(1000)
        self.Notify()

        #self.panel1 = wx.Panel(id=wxID_MAINFORMPANEL1, name='panel1',
        #      parent=self, size=(-1, 50), style=wx.TAB_TRAVERSAL)
        self.panel1 = WbHtmlPanel.create(self)
        self.panel1.SetMinSize((-1, 50))

        self.panel2 = wx.Panel(id=wxID_MAINFORMPANEL2, name='panel2',
              parent=self, size=(-1, -1), style=wx.TAB_TRAVERSAL)

        self.ep = WbEditPanel.create(self.panel2, readonly=True)
        self.staticLine1 = wx.StaticLine(self.panel2)

        self.box = wx.StaticBox(parent=self.panel2, id=wxID_MAINFORMBOX,
              label=_("Browse File Attributes"), name='box')

        self.modify = wx.Button(id=wxID_MAINFORMMODIFY, label=_("Modify"),
              name='modify', parent=self.panel2)
        self.modify.Enable(not self.readonly)
        self.modify.Bind(wx.EVT_BUTTON, self.OnModifyButton,
              id=wxID_MAINFORMMODIFY)

        self.staticText1 = wx.StaticText(id=wxID_MAINFORMSTATICTEXT1,
              label=_("Select File:"), name='staticText1', parent=self.panel2)

        self.dirlist = wx.GenericDirCtrl(id=wxID_MAINFORMDIRLIST, 
              name='dirlist', parent=self.panel2, style=wx.DIRCTRL_SHOW_FILTERS,
              filter="All files (*.*)|*.*|JPEG files (*.jpg)|*.jpg")
        self.tree = self.dirlist.GetTreeCtrl()
        self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OndirlistChanged)


        self._init_sizers()
        self.Fit()
        self.SetSize((-1, 500))

    def __init__(self, parent):
        self.path = ""
        self.readonly = True
        self._init_ctrls(parent)
    
    def Notify(self):
        st = wx.DateTime.Now().Format("%Y-%m-%d    %H:%M:%S")
        self.SetStatusText(st, 1)

    def OnHelpItemaboutMenu(self, event):
        WbAboutDialog.create(self)

    def OnModifyButton(self, event):
        dlg = WbEditDialog.create(self, self.path)
        #dlg.CenterOnScreen()
        val = dlg.ShowModal()
        if val == wx.ID_OK:
            dlg.setFileAttrs()
            self.ep.updateAttrs(self.path, True)
        dlg.Destroy()

    def OndirlistChanged(self, event):
        if event.GetItem() == self.tree.GetRootItem():
            return
        import os
        self.path = self.dirlist.GetPath()
        self.statusBar1.SetStatusText(self.path)
        self.ep.updateAttrs(self.path, True)
        self.readonly = not os.path.isfile(self.path)
        self.modify.Enable(not self.readonly)
        event.Skip()

def Test():
    app = wx.PySimpleApp()
    mainform = create()
    mainform.Show()
    app.MainLoop()

if __name__ == '__main__':
    from Main import installwxgettext
    installwxgettext()
    Test()
